package com.linkstec.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linkstec.bookshop.mapper.BookMapper;
import com.linkstec.bookshop.pojo.Book;
import com.linkstec.bookshop.pojo.Goods;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.BookService;
import com.linkstec.bookshop.service.GoodsService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.BookVo;

/**
 * @ClassName: BookServiceImpl
 * @Description:TODO
 * @author: yanyuan
 * @date: 2018年11月26日 上午10:32:31
 */
@Service
public class BookServiceImpl implements BookService {

	@Autowired
	private BookMapper bookMapper;

	@Autowired
	private GoodsService goodsService;

	@Override
	public PageBean<BookVo> findAllBook(int page) throws RuntimeException {
		PageBean<BookVo> bookPageBean = new PageBean<BookVo>();

		bookPageBean.setPage(page);// 第几页

		int limitPage = 10;
		bookPageBean.setLimitPage(limitPage);// 设置每页10个

		int count = bookMapper.findBookCount();// 查询数量
		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		bookPageBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<BookVo> list = bookMapper.findBookByPage(beginPage, limitPage);

		bookPageBean.setList(list);
		return bookPageBean;
	}

	@Transactional
	@Override
	public void addBook(Book book) throws RuntimeException {
		bookMapper.addBook(book);
		goodsService.add(book.getBookId());
	}

	@Override
	public Book findBookById(int bookId) throws RuntimeException {
		// TODO Auto-generated method stub
		return bookMapper.findBookById(bookId);
	}

	@Transactional
	@Override
	public void update(Book book) throws RuntimeException {
		bookMapper.update(book);
	}

}
